
##############
# POR LINHAS #
##############

import xlrd
import sqlite3
import os

if (os.path.exists('hippie-linhas-TESTE.csv')):
    os.unlink('hippie-linhas-TESTE.csv')

csvfile = open('hippie-linhas-TESTE.csv', 'w')

ESTAB_ENSINO = ''
UNI_ORGANICA = ''
NIVEL_FORMACAO = ''
CURSO_SUP = ''
AREA_EDU_FOR = ''

wb = xlrd.open_workbook("hippie.xls")
for sheetname in wb.sheet_names() :
    sh = wb.sheet_by_name("Sheet1")
    for rownum in range(4, sh.nrows, 1):
		if sh.cell_value(rownum,0) == 'TOTAL':
			break
		else:
			if sh.cell_value(rownum,0) != '':
				ESTAB_ENSINO = sh.cell_value(rownum, 0).encode('latin-1')
				#print 'ENSINO : ', ESTAB_ENSINO
			if sh.cell_value(rownum, 1) != '':
				UNI_ORGANICA = sh.cell_value(rownum, 1).encode('latin-1')
				#print 'UNI : ', UNI_ORGANICA
			if sh.cell_value(rownum,2) != '':
				NIVEL_FORMACAO = sh.cell_value(rownum, 2).encode('latin-1')
				#print 'NIVEL : ', NIVEL_FORMACAO
			if sh.cell_value(rownum,3) != '':
				CURSO_SUP = sh.cell_value(rownum, 3).encode('latin-1')
				#print 'CURSO : ', CURSO_SUP
			if sh.cell_value(rownum,4) != '':
				AREA_EDU_FOR = sh.cell_value(rownum, 4).encode('latin-1')
				#print 'AREA : ', AREA_EDU_FOR
			
			for col in range(sh.ncols):         
				if sh.cell(rownum,col).value == "-":
						csvfile.write("0.0")
				elif isinstance(sh.cell(rownum,col).value, float):
					csvfile.write(str(sh.cell(rownum,col).value))

				else:
					if (sh.cell(rownum,col).value) == '' and col == 0:
						csvfile.write(ESTAB_ENSINO) # se coluna 0 e vazia
					elif (sh.cell(rownum,col).value) == '' and col == 1:
						csvfile.write(UNI_ORGANICA) # se coluna 1 e vazia
					elif (sh.cell(rownum,col).value) == '' and col == 2:
						csvfile.write(NIVEL_FORMACAO) # se coluna 2 e vazia
					elif (sh.cell(rownum,col).value) == '' and col == 3:
						csvfile.write(CURSO_SUP) # se coluna 3 e vazia
					elif (sh.cell(rownum,col).value) == '' and col == 4:
						csvfile.write(AREA_EDU_FOR) # se coluna 4 e vazia
					else:
						csvfile.write((sh.cell(rownum,col).value).encode('latin-1'))
				csvfile.write(' | ')
			csvfile.write('\n')
csvfile.write("########## FIM ##########")
print("########## FIM ##########")
csvfile.close()

###############
# POR COLUNAS #
###############

'''
import xlrd

csvfile = open('hippie.csv', 'w')

wb = xlrd.open_workbook("hippie.xls")

for sheetname in wb.sheet_names() :
    sh = wb.sheet_by_name(sheetname)
    for rownum in range(sh.nrows) :
        print sh.row_values(rownum)
        values = []        
        for col in range(sh.ncols):
            if isinstance(sh.cell(rownum,col).value, float):
                csvfile.write(str(sh.cell(rownum,col).value))
            else:
                csvfile.write((sh.cell(rownum,col).value).encode('latin-1'))
    csvfile.write('\n')
csvfile.close()
'''
